package com.coocaa.ops.admin.v1.plugin.service;

import com.coocaa.ops.admin.v1.plugin.biz.IPluginInfoBiz;
import com.coocaa.ops.admin.v1.plugin.model.output.PluginInfoModel;
import com.coocaa.ops.admin.v1.plugin.model.query.PluginQueryModel;
import com.coocaa.ops.admin.v1.plugin.util.PluginInfoConverter;
import com.coocaa.ops.admin.v1.plugin.util.PluginQueryConverter;
import com.coocaa.ops.common.core.base.model.PageResultModel;
import com.coocaa.ops.common.core.biz.module.constant.ModuleTypeConstants;
import com.coocaa.ops.common.core.biz.plugin.entity.PluginInfoEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author tangminghong
 * @date 2020-06-28
 */
@Service
public class PluginServiceImpl implements IPluginService {

    /**
     * 实现类注入，key为bean名，需要在实现类指定名称为{@link ModuleTypeConstants}中对应的的名称
     */
//    @Autowired
//    private Map<String, IPluginBusinessService> pluginBusinessServiceMap;

    @Autowired
    private IPluginInfoBiz pluginInfoBiz;


    @Override
    public PageResultModel<PluginInfoModel> pageList(PluginQueryModel queryModel) {
        if (StringUtils.isBlank(queryModel.getModuleType())) {
            return PageResultModel.getPageResultModel(0, null);
        }
        return null;
        //根据不同的moduleType调用不同业务的服务
//        IPluginBusinessService pluginBusinessService = pluginBusinessServiceMap.get(queryModel.getModuleType());
//        if (pluginBusinessService == null) {
//            //如果没有相关业务的实现类，则统一到数据库获取
//            return getFromDataBase(queryModel);
//        }
//        return pluginBusinessService.pageList(queryModel.getPage(), queryModel.getRows());
    }

    private PageResultModel<PluginInfoModel> getFromDataBase(PluginQueryModel queryModel) {
        PageResultModel<PluginInfoEntity> entityResult = pluginInfoBiz.getCommonPageList(PluginQueryConverter.getQueryMap(queryModel), false);
        List<PluginInfoEntity> entityList = entityResult.getResultList();
        List<PluginInfoModel> modelList = PluginInfoConverter.convertToModel(entityList);
        return PageResultModel.getPageResultModel(entityResult.getTotal(), modelList);

    }

}
